package com.wesley.test.controller;

import com.wesley.test.entity.User;
import com.wesley.test.form.UserForm;
import com.wesley.test.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping(value = "/user")
public class UserController {

    @Autowired
    private UserService userService;

    @GetMapping(value = "/get/all")
    public List<User> getAll() {
        return userService.getAllUser();
    }

    @GetMapping(value = "/get/byName")
    public User getByName(String name){
        return userService.getByName(name);
    }


    @PostMapping(value = "/save")
    public Map<String,Object> save(@RequestBody User user) {
        Map<String, Object> map = new HashMap<String, Object>();
        try {
            userService.saveUser(user);
            map.put("tag", true);
        }catch (Exception e) {
            e.printStackTrace();
            map.put("tag", false);
        }
        return map;
    }



    @GetMapping(value = "/get/search")
    public List<User> search(UserForm userForm) {
        return userService.findUser(userForm);
    }

    
    @DeleteMapping(value = "/delete")
    public Map<String, Object> deleteById(int id) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		try {
			userService.deleteById(id);
			resultMap.put("tag", true);
		} catch (Exception e) {
			e.printStackTrace();
			resultMap.put("tag", false);
			resultMap.put("message", e.getMessage());
		}
    	return resultMap;
	}


}
